package pt.inescid.components.policyengine.heimdall.pdp.base;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.commons.TimeUtilities;
import pt.inescid.components.policyengine.heimdall.pap.base.PurgeDefinition;

public class PurgeInstance {

	private PurgeDefinition definition;
	private RuleInstance purgeFilter;
	
	
	private long lastPurgeTime = 0;
	private long every = 0;
	
	
//  ----------------------------------------------------------
//  ------------------------   constructor   ------------------------
//  ----------------------------------------------------------
	public PurgeInstance(PurgeDefinition definition) {
		this.definition = definition;
		
		this.lastPurgeTime = TimeUtilities.getCurrentTime();
		this.every = TimeUtilities.parseTimeQty(
				definition.getEvery());
	}

	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      shouldPurge       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public boolean shouldPurge(EventObj ce) {
		
		ModelInstance currentModel = new ModelInstance(ce);
		purgeFilter = RuleInstance.getInstance(
				definition.getPurgeFilter(),
				currentModel,
				null);

		if (purgeFilter == null) {
			return (false);
		}
		else {
			return (((Boolean) purgeFilter.evaluate(currentModel,
					false)).
					booleanValue());
		}
	} // shouldPurge
		


	public PurgeDefinition getDefinition() {
		return definition;
	}


	public long getEvery() {
		return every;
	}


	public long getLastPurgeTime() {
		return lastPurgeTime;
	}


	public void setDefinition(PurgeDefinition definition) {
		this.definition = definition;
	}


	public void setEvery(long every) {
		this.every = every;
	}


	public void setLastPurgeTime(long lastPurgeTime) {
		this.lastPurgeTime = lastPurgeTime;
	}


	
	
}; // ReevaluationInstance
